package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.databinding.ActivitySelVideoBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.PermissionUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelVideoActivity extends BaseAc<ActivitySelVideoBinding> {

    private SelVideoAdapter selVideoAdapter;
    private int oldPosition = 0;
    private String selPath = "";
    private boolean again;
    public static int type=0; // 0格式播放 1滤镜 2剪切 3旋转 4倒放 5拼接 6文字 7背景 8裁剪 9变速 10格式转换


    @Override
    protected int onCreate() {
        return R.layout.activity_sel_video;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelVideoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.tvSelVideoImport.setOnClickListener(this);
        mDataBinding.rvSelVideoList.setLayoutManager(new GridLayoutManager(mContext, 3));
        selVideoAdapter = new SelVideoAdapter();
        mDataBinding.rvSelVideoList.setAdapter(selVideoAdapter);
        selVideoAdapter.setOnItemClickListener(this);
        //判断是否是视频拼接再次进入选择视频界面
        again = getIntent().getBooleanExtra(Content.Again, false);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvSelVideoImport:
                if (selPath.isEmpty()){
                    ToastUtils.showShort(R.string.please_sel_import_video);
                    return;
                }
                if (type==0){
                    PlayFormatActivity.videoPath=selPath;
                    startActivity(PlayFormatActivity.class);
                }else if (type==1){
                    VideoFilterActivity.videoPath=selPath;
                    startActivity(VideoFilterActivity.class);
                }else if (type==2){
                    VideoCutActivity.videoPath=selPath;
                    startActivity(VideoCutActivity.class);
                }else if (type==3){
                    VideoRotateActivity.videoPath=selPath;
                    startActivity(VideoRotateActivity.class);
                }else if (type==4){
                    VideoUpendActivity.videoPath=selPath;
                    startActivity(VideoUpendActivity.class);
                }else if (type==5){
                    if (again) {//视频拼接再次选择视频
                        Intent intent = new Intent();
                        intent.putExtra(Content.VideoPath, selPath);
                        setResult(RESULT_OK, intent);
                        finish();
                        return;
                    }
                    VideoSpliceActivity.videoPath=selPath;
                    startActivity(VideoSpliceActivity.class);
                }else if (type==6){
                    ViewTextActivity.videoPath=selPath;
                    startActivity(ViewTextActivity.class);
                }else if (type==7){
                    VideoBgActivity.videoPath=selPath;
                    startActivity(VideoBgActivity.class);
                }else if (type==8){
                    VideoTailorActivity.videoPath=selPath;
                    startActivity(VideoTailorActivity.class);
                }else if (type==9){
                    VideoSpeedActivity.videoPath=selPath;
                    startActivity(VideoSpeedActivity.class);
                }else if (type==10){
                    VideoFormatActivity.videoPath=selPath;
                    startActivity(VideoFormatActivity.class);
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        selVideoAdapter.getItem(oldPosition).setChecked(false);
        selVideoAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        selVideoAdapter.getItem(position).setChecked(true);
        selVideoAdapter.notifyItemChanged(position);
        selPath = selVideoAdapter.getItem(position).getPath();
    }

    @Override
    protected void initData() {
        getPermission();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvSelVideoList.setVisibility(View.GONE);
                        mDataBinding.llSelVideoNoData.setVisibility(View.GONE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(getApplicationContext(), PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    selVideoAdapter.setList(selectMediaEntities);
                    mDataBinding.rvSelVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.llSelVideoNoData.setVisibility(View.GONE);
                    mDataBinding.llSelVideoBottom.setVisibility(View.VISIBLE);
                } else {
                    mDataBinding.rvSelVideoList.setVisibility(View.GONE);
                    mDataBinding.llSelVideoNoData.setVisibility(View.VISIBLE);
                    mDataBinding.llSelVideoBottom.setVisibility(View.GONE);
                }
            }
        });
    }

}